*************************************************************************************************************
* Table 7: Effects of reconciliation
* This table reports estimates of the effect of reconciliation on measures of the value disbursed by the governmen received by recipients, and the dfference separately for treatment and control areas
*************************************************************************************************************


**************************************************************
* 0) mean entitlement
**************************************************************


use "${SurveyDataDir}/JH_ePOS_HH_reconciliation_DataforAnalysis.dta",clear

*include only if surveyed or ghosts
keep if surveyed==1|ghost_final==1


svyset [pw = pweight]


count if ghost_final == 1
scalar ghosts = r(N)
scalar obs = 3960   

*Get relative weights of AAY and PH rationcard holders by whether RC is in an urban area

forval i=0/1{
sum pweight if rationcardtype == "AAY" & isurban == 0 & treatment==`i'
scalar AAY_weight0_`i' = r(sum)

sum pweight if rationcardtype == "PH" & isurban == 0 & treatment==`i'
scalar PH_weight0_`i' = r(sum)

sum pweight if rationcardtype == "AAY" & isurban == 1 & treatment==`i'
scalar AAY_weight1_`i' = r(sum)

sum pweight if rationcardtype == "PH" & isurban == 1 & treatment==`i'
scalar PH_weight1_`i' = r(sum)


}

***************************************************************
*store mean entitlement in place holder regression results

loc ration "totalrec totalnonrec"
local month_list "nov17 oct17 sep17 aug17 jul17 jun17 may17 apr17 mar17 feb17 jan17"

foreach rat of local ration {

preserve


keep value_`rat'_*17 treatment strata pweight block_code uid rationcardtype stat_ent_value_`rat'* rationcardtype isurban

loc n=11
foreach mon of local month_list{
rename value_`rat'_`mon' value_`rat'`n'
rename stat_ent_value_`rat'_`mon' stat_ent_value_`rat'`n'

loc n=`n'-1

}

reshape long value_`rat' stat_ent_value_`rat', i(uid) j(month)

count


****************************************
* Treatment/Control 
****************************************

forvalues i=0/1{

*Statutory NIC entitlement
qui svy: mean stat_ent_value_`rat' if rationcardtype == "AAY" & isurban == 0 & treatment==`i'
qui estat sd
matrix MeanAAY0s_`i' = r(mean)

qui svy: mean stat_ent_value_`rat' if rationcardtype == "PH" & isurban == 0 & treatment==`i'
qui estat sd
matrix MeanPH0s_`i' = r(mean)

qui svy: mean stat_ent_value_`rat' if rationcardtype == "AAY" & isurban == 1 & treatment==`i'
qui estat sd
matrix MeanAAY1s_`i' = r(mean)

qui svy: mean stat_ent_value_`rat' if rationcardtype == "PH" & isurban == 1 & treatment==`i'
qui estat sd
matrix MeanPH1s_`i' = r(mean)

*Calculate mean entitlement weighted by sampling probabilities 
scalar avg_stat_ent_value_`rat'_`i' = (AAY_weight0_`i'*`=MeanAAY0s_`i'[1,1]' + PH_weight0_`i'*`=MeanPH0s_`i'[1,1]' + AAY_weight1_`i'*`=MeanAAY1s_`i'[1,1]' + PH_weight1_`i'*`=MeanPH1s_`i'[1,1]')/(AAY_weight0_`i' + PH_weight0_`i' + AAY_weight1_`i' + PH_weight1_`i')

*place holder regression 
eststo `rat'_v1BL_`i': xi: reg value_`rat' treatment i.strata [pw = pweight], cluster(block_code)
*add summary stat (mean entitlement) to be displayed on top of table 
estadd scalar stat_ent_mean = avg_stat_ent_value_`rat'_`i'
}



****************************************
* Difference
****************************************
*place holder regression 
eststo `rat'_v1BL_d: xi: reg value_`rat' treatment i.strata [pw = pweight], cluster(block_code)
*calculate difference in mean
estadd scalar stat_ent_mean = avg_stat_ent_value_`rat'_1-avg_stat_ent_value_`rat'_0


restore
}

*Table set up
loc tabname "${OutputDir}/Table7.tex" 
cd "${adoDir}"
loc nc=6	

*************************************************************************
*Header row for table	
*************************************************************************
	
MultiPartTabStart, ///
			ncol(`nc') tabname(`tabname') ///
			colnames("Treatment" "Control" "Difference" "Treatment" "Control" "Difference" ) width("\hsize")  ///
			mgroups("Reconciled" "Unreconciled")	///
			pattern(1 0 0 1 0 0) 
			

*************************************************************************
*Mean entitlement row 
*************************************************************************
	
MultiPartTabPanelStartEntitle, ///
			ncol(`nc') tabname(`tabname') ///
			panelstring("")
			
			estimates restore *totalrec_v1BL_1
			estimates restore *totalrec_v1BL_0
			estimates restore *totalrec_v1BL_d
			estimates restore *totalnonrec_v1BL_1
			estimates restore *totalnonrec_v1BL_0
			estimates restore *totalnonrec_v1BL_d
			
MultiPartTabPanelEndEntitle, ///
				ncol(`nc') tabname(`tabname') ///
				models("*totalrec_v1BL_1 *totalrec_v1BL_0 *totalrec_v1BL_d *totalnonrec_v1BL_1 *totalnonrec_v1BL_0 *totalnonrec_v1BL_d") ///
				drop(treatment _Ist* _cons) ///
				cells(b) ///
				stats(stat_ent_mean, label("\textit{Mean entitlement}") fmt(0))
	


	
	
**************************************************************
* 1) Disbursement
**************************************************************


*prepare program to stack difference results in a single regression 
*******************************************************************************
program mydisp, eclass
         matrix betas=e(b)
         matrix colnames betas = "reconciliation" "recXtmint" "index"  "post_reconciliation"
         mat list betas
         ereturn repost b = betas, rename
 end


*******************************************************************************

*******************************************************************************
*FPS-level analysis
*******************************************************************************
********************************************
* Use EL2/3 disbursement data
********************************************
use "${AdminDataDir}/allocation_FPSlevel_jan17_nov17.dta",clear


******************************************************
*Analysis
******************************************************


*******
* Value
*******
local ration1 "totalr totalnr"
local ration "totalrec totalnonrec rice wheat sugar salt kero"
local ration0 "rice wheat sugar salt kero"
local month_list "nov17 oct17 sep17 aug17 jul17 jun17 may17 apr17 mar17 feb17 jan17"


*obs counts by T/C
scalar obs_all =  9265*11
scalar obs_all_1 =  6297 *11
scalar obs_all_0 =  2968 *11

foreach rat of local ration1 {
preserve

keep dis_value_perRC_`rat'_*17 admin_dealer_id index_* reconciliation_* post_reconciliation_* rc_count* block_code ent* treatment

local m = 11
foreach mon of local month_list{
rename dis_value_perRC_`rat'_`mon' dis_value_perRC_`rat'`m'
rename index_`mon' index`m' 
rename reconciliation_`mon' reconciliation`m'
rename post_reconciliation_`mon' post_reconciliation`m'
rename rc_count_`mon' rc_count`m'
rename ent_value_perRC_`rat'_`mon' ent_value_perRC`m'


local m = `m' - 1
}


reshape long dis_value_perRC_`rat' index reconciliation post_reconciliation rc_count ent_value_perRC, i(admin_dealer_id) j(month)
gen recXtmint = reconciliation*(index - 7)


*******************************************
* Treatment and control 
*******************************************

forval i=0/1{

	*Calculate Jan Mean 
	di in red "`i'"
	qui: summarize dis_value_perRC_`rat' if month == 1 & treatment==`i' [aweight=rc_count]
	
	matrix Mean0_`i' = r(mean)


	qui: mean ent_value_perRC if treatment==`i' 
	qui estat sd
	matrix Mean0e_`i' = r(mean)
    
	*Main result estimation 
	eststo `rat'_`i': reg dis_value_perRC_`rat' index reconciliation recXtmint post_reconciliation[aweight=rc_count] if treatment==`i', cluster(admin_dealer_id)
	estadd scalar january_mean = `=Mean0_`i'[1,1]'
	estadd scalar percent_obs 100*e(N)/obs_all_`i'
	estadd scalar stat_ent_mean = `=Mean0e_`i'[1,1]'

	*Place holder estimation for suest (no cluster, add cluster in suest step)
	eststo `rat'_s_`i': reg dis_value_perRC_`rat' index reconciliation recXtmint post_reconciliation[aweight=rc_count] if treatment==`i'


}
	
	
*******************************************
* Difference
*******************************************
eststo temp: suest `rat'_s_1 `rat'_s_0, cluster(admin_dealer_id)

    *test of coefficient (for double checking numbers only, won't be displayed in table)
	test [`rat'_s_1_mean]reconciliation = [`rat'_s_0_mean]reconciliation
	test [`rat'_s_1_mean]recXtmint = [`rat'_s_0_mean]recXtmint

	*Storing test result in fake regression
	eststo reconciliation_`rat': lincomest[`rat'_s_1_mean]reconciliation - [`rat'_s_0_mean]reconciliation
		estimates restore temp
	eststo recXtmint_`rat': lincomest[`rat'_s_1_mean]recXtmint - [`rat'_s_0_mean]recXtmint
		estimates restore temp
	eststo index_`rat' : lincomest[`rat'_s_1_mean]index - [`rat'_s_0_mean]index
		estimates restore temp
	eststo post_reconciliation_`rat': lincomest[`rat'_s_1_mean]post_reconciliation - [`rat'_s_0_mean]post_reconciliation

*append stored results in a single regression
eststo `rat'_d: appendmodels reconciliation_`rat' recXtmint_`rat' index_`rat' post_reconciliation_`rat'
eststo `rat'_d: mydisp
	
restore
}




*********************************************
*Value disbursed panel
*********************************************

			
MultiPartTabPanelStart, ///
			ncol(`nc') tabname(`tabname') ///
			panelstring("Panel A: Value disbursed")
			
			
MultiPartTabPanelEnd, ///
				ncol(`nc') tabname(`tabname') ///
				models("totalr_1 totalr_0 totalr_d totalnr_1 totalnr_0 totalnr_d") ///
				cells(b(star fmt(%12.2g) ) se(par(( )) fmt(%12.2g) ) ) ///
				stats(january_mean N percent_obs, label("January 2017 mean" "Observations" "\% of frame") fmt(%12.2g %15.0fc 0)) starlevels( * 0.10 ** 0.05 *** 0.01) ///
				order(reconciliation recXtmint)  ///
				varlabels(reconciliation "Reconciliation" recXtmint "Reconciliation * Month" ///
				,elist(reconciliation \addlinespace recXtmint \addlinespace )) ///
				keep(reconciliation recXtmint)







program drop mydisp
**************************************************************
* 2) receipt
**************************************************************


*define program 
*******************************************************************************
program mydisp, eclass
         matrix betas=e(b)
         matrix colnames betas = "reconciliation" "recXtmint" "index"  "post_rec"
         mat list betas
         ereturn repost b = betas, rename
 end


*******************************************************************************
***************************************************************
* Analysis
***************************************************************

use "${SurveyDataDir}/JH_ePOS_HH_reconciliation_DataforAnalysis.dta",clear


*include only if surveyed or ghosts
keep if surveyed==1|ghost_final==1
						

***************************************************************
* Value
***************************************************************

local ration1 "totalrec totalnonrec"
local ration "totalrec totalnonrec rice wheat sugar salt kero"
local ration0 "rice wheat sugar salt kero"



scalar obs_all =  3960*11
scalar obs_all_1 =   2610  *11
scalar obs_all_0 =  1350*11

local month_list "nov17 oct17 sep17 aug17 jul17 jun17 may17 apr17 mar17 feb17 jan17"
foreach rat of local ration1 {
preserve

svyset fps_uid [pw=pweight]

keep value_`rat'_*17 uid fps_uid index_* reconciliation_* post_reconciliation_* ghost_final pweight treatment

local m = 11
foreach mon of local month_list{
rename value_`rat'_`mon' value_`rat'`m'
rename index_`mon' index`m' 
rename reconciliation_`mon' reconciliation`m'
rename post_reconciliation_`mon' post_reconciliation`m'

local m = `m' - 1
}


reshape long value_`rat' index reconciliation post_reconciliation, i(uid) j(month)
gen recXtmint = reconciliation*(index - 7)

*******************************************
* Treatment and control 
*******************************************
forval i=0/1{

di in red "`i'"

	*Calculate Jan Mean
	qui svy: mean value_`rat' if month == 1 & treatment==`i'
	qui estat sd
	matrix Mean0_`i' = r(mean) 
	
	*Main result estimation 
	eststo `rat'_`i': reg value_`rat' index reconciliation recXtmint post_reconciliation [pw = pweight] if treatment==`i', cluster(fps_uid)
	estadd scalar january_mean = `=Mean0_`i'[1,1]'
	estadd scalar percent_obs 100*e(N)/obs_all_`i'
	
	
	
	*Place holder estimation for suest (no cluster/weighting)
	eststo `rat'_s_`i': svy:reg value_`rat' index reconciliation recXtmint post_reconciliation if treatment==`i'


}

*******************************************
* Difference
*******************************************
eststo temp: suest `rat'_s_1 `rat'_s_0


    *test of coefficient
	test [`rat'_s_1]reconciliation = [`rat'_s_0]reconciliation
	test [`rat'_s_1]recXtmint = [`rat'_s_0]recXtmint

	*Storing test result in fake regression
	eststo reconciliation_`rat': lincomest[`rat'_s_1]reconciliation - [`rat'_s_0]reconciliation
		estimates restore temp
	eststo recXtmint_`rat': lincomest[`rat'_s_1]recXtmint - [`rat'_s_0]recXtmint
		estimates restore temp
	eststo index_`rat' : lincomest[`rat'_s_1]index - [`rat'_s_0]index
		estimates restore temp
	eststo post_rec_`rat': lincomest[`rat'_s_1]post_reconciliation - [`rat'_s_0]post_reconciliation

*manually count obs 
count if !missing(value_`rat')  
scalar used_obs=r(N) 
	
*append stored results 
eststo `rat'_d: appendmodels reconciliation_`rat' recXtmint_`rat' index_`rat' post_rec_`rat'
eststo `rat'_d: mydisp
	

restore
}



*********************************************
*Value received panel	
*********************************************

MultiPartTabPanelStart, ///
			ncol(`nc') tabname(`tabname') ///
			panelstring("Panel B: Value received")
			
			
MultiPartTabPanelEnd, ///
				ncol(`nc') tabname(`tabname') ///
				models("totalrec_1 totalrec_0 totalrec_d totalnonrec_1 totalnonrec_0 totalnonrec_d") ///
				cells(b(star fmt(%12.2g) ) se(par(( )) fmt(%12.2g) ) ) ///
				stats(january_mean N percent_obs, label("January 2017 mean" "Observations" "\% of sample") fmt(%12.2g %15.0fc 0)) starlevels( * 0.10 ** 0.05 *** 0.01) ///
				order(reconciliation recXtmint)  ///
				varlabels(reconciliation "Reconciliation" recXtmint "Reconciliation * Month" ///
				,elist(reconciliation \addlinespace recXtmint \addlinespace )) ///
				keep(reconciliation recXtmint)

				
				
				
						
				
program drop mydisp
**************************************************************
* 3) leakage
**************************************************************



use "${SurveyDataDir}/JH_ePOS_HH_reconciliation_DataforAnalysis.dta",clear



*define program 

*******************************************************************************
program mydisp, eclass
         matrix betas=e(b)
         matrix colnames betas = "reconciliation" "recXtmint" "index"  "post_rec"
         mat list betas
         ereturn repost b = betas, rename
 end
********************************************************************************


* Leakage estimates
* SUR of disbursements_perRC and quantities received with BL lags and then estimate differences between estimate for disbursements_perRC and quantities received
********************************************************************************

********************************************
* Use EL1/2/3 disbursement data
********************************************
use "${AdminDataDir}/allocation_blocklevel_jan17_nov17.dta",clear


*Merge HH survey data

preserve
use "${SurveyDataDir}/JH_ePOS_HH_reconciliation_DataforAnalysis.dta",clear

*include only if surveyed or ghosts
keep if surveyed==1|ghost_final==1

*Keep only variables needed in analysis
keep district_code block_code fps_uid uid quantity* value* pweight isurban 

tempfile HHdata
save `HHdata'
restore

merge 1:m district_code block_code isurban using `HHdata',update  


********************************************
* Value
********************************************
local ration "total rice wheat sugar salt kero"
local month_list "nov17 oct17 sep17 aug17 jul17 jun17 may17 apr17 mar17 feb17 jan17"

*rename value vars 
foreach mon of local month_list{
	
	rename value_totalrec_`mon' value_totalr_`mon'
	rename value_totalnonrec_`mon' value_totalnr_`mon'

}

scalar obs_all =  3960*11
scalar obs_all_1 =   2610*11
scalar obs_all_0 =  1350*11


****total values
local rationt "totalr totalnr"
local month_list "nov17 oct17 sep17 aug17 jul17 jun17 may17 apr17 mar17 feb17 jan17"


foreach rat of local rationt{
	preserve
	
	local month_list "nov17 oct17 sep17 aug17 jul17 jun17 may17 apr17 mar17 feb17 jan17"
	svyset fps_uid [pw=pweight]

	keep dis_value_perRC_`rat'_*17 value*17 strata block_code index_* reconciliation_* post_reconciliation_* uid fps_uid pweight treatment

	local m = 11

	foreach mon of local month_list{
		rename dis_value_perRC_`rat'_`mon' dis_value_perRC_`rat'`m'
		rename value_`rat'_`mon' value_`rat'`m'
		rename index_`mon' index`m' 
		rename reconciliation_`mon' reconciliation`m'
		rename post_reconciliation_`mon' post_reconciliation`m'

		local m = `m' - 1
	}
	reshape long value_`rat' dis_value_perRC_`rat' index reconciliation post_reconciliation, i(uid) j(month)
	
	gen recXtmint = reconciliation*(index - 7)
	
***********************************************************
*Treatment/Control
***********************************************************

forval i=0/1{

	*Disbursement per RC
	
		*Jan Mean
		qui svy: mean dis_value_perRC_`rat' if month == 1 & treatment==`i'
		qui estat sd
		matrix Mean0d_`i'`rat' = r(mean)
	
		*Main estimation
		eststo `rat'd_`i': svy:reg dis_value_perRC_`rat' index reconciliation recXtmint post_reconciliation if treatment==`i'
		estadd scalar january_mean = `=Mean0d_`i'`rat'[1,1]'
		estadd scalar percent_obs 100*e(N)/obs_all
		
	*Value received reported by HHs
	
		*Jan Mean
		qui svy: mean value_`rat' if month == 1 & treatment==`i'
		qui estat sd
		matrix Mean0r_`i'`rat' = r(mean)
		
		*Main estimation
		eststo `rat'r_`i': svy:reg value_`rat' index reconciliation recXtmint post_reconciliation if treatment==`i'
		estadd scalar january_mean = `=Mean0r_`i'`rat'[1,1]'
		estadd scalar percent_obs 100*e(N)/obs_all
		
	
	*Jointly estimate leakage
	eststo temp: suest `rat'd_`i' `rat'r_`i'
	*scalar r2_adj = e(r2_a)
	
	eststo reconciliation_`rat'_`i': lincomest[`rat'd_`i']reconciliation - [`rat'r_`i']reconciliation
	estimates restore temp
	
	eststo recXtmint_`rat'_`i': lincomest[`rat'd_`i']recXtmint - [`rat'r_`i']recXtmint
	estimates restore temp
	
	eststo index_`rat'_`i' : lincomest[`rat'd_`i']index - [`rat'r_`i']index
	estimates restore temp
	
	eststo post_rec_`rat'_`i': lincomest[`rat'd_`i']post_reconciliation - [`rat'r_`i']post_reconciliation
	
	count if (!missing(dis_value_perRC_`rat') & !missing(value_`rat')) & treatment==`i'
	scalar used_obs=r(N) 
	
	eststo all_`rat'_`i': appendmodels reconciliation_`rat'_`i' recXtmint_`rat'_`i' index_`rat'_`i' post_rec_`rat'_`i'
	eststo all_`rat'_`i': mydisp
	
	
	*Calculate leakage mean to be used in difference column 
	scalar Mean0L_`i'=`=Mean0d_`i'`rat'[1,1]' - `=Mean0r_`i'`rat'[1,1]'
	
	estadd scalar january_mean = `=Mean0d_`i'`rat'[1,1]' - `=Mean0r_`i'`rat'[1,1]'
	
	estadd scalar N=used_obs
	estadd scalar percent_obs 100*used_obs/obs_all_`i'
	
	}

	
***********************************************************
*Difference
***********************************************************	
di in red "suest starts here"

*Gen interaction terms
gen indexXtrt = index*treatment
gen reconciliationXtrt=reconciliation*treatment
gen recXtmintXtrt=recXtmint*treatment
gen post_recXtrt=post_reconciliation*treatment

*value disbursed
eststo `rat'd_d: svy:reg dis_value_perRC_`rat' index reconciliation recXtmint post_reconciliation indexXtrt reconciliationXtrt recXtmintXtrt post_recXtrt treatment

*value received
eststo `rat'r_d: svy:reg value_`rat' index reconciliation recXtmint post_reconciliation indexXtrt reconciliationXtrt recXtmintXtrt post_recXtrt treatment


*jointly estimate leakage
eststo temp: suest `rat'd_d `rat'r_d
	
	eststo reconciliation_`rat'_d: lincomest[`rat'd_d]reconciliationXtrt - [`rat'r_d]reconciliationXtrt
	estimates restore temp
	
	eststo recXtmint_`rat'_d: lincomest[`rat'd_d]recXtmintXtrt - [`rat'r_d]recXtmintXtrt
	estimates restore temp
	
	eststo index_`rat'_d: lincomest[`rat'd_d]indexXtrt - [`rat'r_d]indexXtrt
	estimates restore temp
	
	eststo post_rec_`rat'_d: lincomest[`rat'd_d]post_recXtrt - [`rat'r_d]post_recXtrt

eststo all_`rat'_d: appendmodels reconciliation_`rat'_d recXtmint_`rat'_d index_`rat'_d post_rec_`rat'_d
eststo all_`rat'_d: mydisp
	

restore
}


cd "${adoDir}"

			
MultiPartTabPanelStart, ///
			ncol(`nc') tabname(`tabname') ///
			panelstring("Panel C: Leakage")
			
			
MultiPartTabPanelEnd, ///
				ncol(`nc') tabname(`tabname') ///
				models("all_totalr_1 all_totalr_0 all_totalr_d all_totalnr_1 all_totalnr_0 all_totalnr_d") ///
				cells(b(star fmt(%12.2g) ) se(par(( )) fmt(%12.2g) ) ) ///
				stats(january_mean N percent_obs, label("January 2017 mean" "Observations" "\% of sample") fmt(%12.2g %15.0fc 0)) starlevels( * 0.10 ** 0.05 *** 0.01) ///
				order(reconciliation reconciliationXtmint)  ///
				varlabels(reconciliation "Reconciliation" recXtmint "Reconciliation * Month" ///
				,elist(reconciliation \addlinespace recXtmint \addlinespace )) ///
				keep(reconciliation recXtmint)				
				
				
				
MultiPartTabEnd, ///
			ncol(`nc') tabname(`tabname') 
			
program drop mydisp
********************************************************************************
* Run analysis found in XXX.do and add as a new panel

*define program 

*******************************************************************************

use "${SurveyDataDir}/JH_ePOS_HH_reconciliation_DataforAnalysis.dta",clear


*include only if surveyed or ghosts
keep if surveyed==1|ghost_final==1
						
program mydisp, eclass
         matrix betas=e(b)
         matrix colnames betas = "reconciliation" "recXtmint" "index"  "post_rec"
         mat list betas
         ereturn repost b = betas, rename
 end
 
***************************************************************
* Value
***************************************************************

local ration1 "totalrec totalnonrec"
local ration "totalrec totalnonrec rice wheat sugar salt kero"
local ration0 "rice wheat sugar salt kero"

scalar obs_all =  3960*11
scalar obs_all_1 =   2610  *11
scalar obs_all_0 =  1350*11

local month_list "nov17 oct17 sep17 aug17 jul17 jun17 may17 apr17 mar17 feb17 jan17"
foreach rat of local ration1 {
preserve

svyset fps_uid [pw=pweight]

keep value_`rat'_*17 uid fps_uid index_* reconciliation_* post_reconciliation_* ghost_final pweight treatment

local m = 11
foreach mon of local month_list{
rename value_`rat'_`mon' value_`rat'`m'
rename index_`mon' index`m' 
rename reconciliation_`mon' reconciliation`m'
rename post_reconciliation_`mon' post_reconciliation`m'

local m = `m' - 1
}


reshape long value_`rat' index reconciliation post_reconciliation, i(uid) j(month)
gen recXtmint = reconciliation*(index - 7)

*generate indicator for if received positive value
gen value_`rat'0 = (value_`rat' > 0)
replace value_`rat'0 = . if missing(value_`rat')


drop value_`rat'

*******************************************
* Treatment and control 
*******************************************
forval i=0/1{

di in red "`i'"

	*Calculate Jan Mean
	qui svy: mean value_`rat'0 if month == 1 & treatment==`i'
	qui estat sd
	matrix Mean0_`i' = r(mean) 
	
	*Main result estimation 
	eststo `rat'_`i': reg value_`rat'0 index reconciliation recXtmint post_reconciliation [pw = pweight] if treatment==`i', cluster(fps_uid)
	estadd scalar january_mean = `=Mean0_`i'[1,1]'
	estadd scalar percent_obs 100*e(N)/obs_all_`i'
	
	
	
	*Place holder estimation for suest (no cluster/weighting)
	eststo `rat'_s_`i': svy:reg value_`rat'0 index reconciliation recXtmint post_reconciliation if treatment==`i'


}

*******************************************
* Difference
*******************************************
eststo temp: suest `rat'_s_1 `rat'_s_0


    *test of coefficient
	test [`rat'_s_1]reconciliation = [`rat'_s_0]reconciliation
	test [`rat'_s_1]recXtmint = [`rat'_s_0]recXtmint

	*Storing test result in fake regression
	eststo reconciliation_`rat': lincomest[`rat'_s_1]reconciliation - [`rat'_s_0]reconciliation
		estimates restore temp
	eststo recXtmint_`rat': lincomest[`rat'_s_1]recXtmint - [`rat'_s_0]recXtmint
		estimates restore temp
	eststo index_`rat' : lincomest[`rat'_s_1]index - [`rat'_s_0]index
		estimates restore temp
	eststo post_rec_`rat': lincomest[`rat'_s_1]post_reconciliation - [`rat'_s_0]post_reconciliation

*manually count obs 
count if !missing(value_`rat'0)  
scalar used_obs=r(N) 
	
*append stored results 
eststo `rat'_d: appendmodels reconciliation_`rat' recXtmint_`rat' index_`rat' post_rec_`rat'
eststo `rat'_d: mydisp
	

restore
}
			
cd "${adoDir}"
			
*********************************************
*Value received panel	
*********************************************

MultiPartTabPanelStart, ///
			ncol(`nc') tabname(`tabname') ///
			panelstring("Panel D: Reconciliation effects at the extensive margin")
			
			
MultiPartTabPanelEnd, ///
				ncol(`nc') tabname(`tabname') ///
				models("totalrec_1 totalrec_0 totalrec_d totalnonrec_1 totalnonrec_0 totalnonrec_d") ///
				cells(b(star fmt(%12.2g) ) se(par(( )) fmt(%12.2g) ) ) ///
				stats(january_mean N percent_obs, label("January 2017 mean" "Observations" "\% of sample") fmt(%12.2g %15.0fc 0)) starlevels( * 0.10 ** 0.05 *** 0.01) ///
				order(reconciliation recXtmint)  ///
				varlabels(reconciliation "Reconciliation" recXtmint "Reconciliation * Month" ///
				,elist(reconciliation \addlinespace recXtmint \addlinespace )) ///
				keep(reconciliation recXtmint)
					
				
MultiPartTabEnd, ///
			ncol(`nc') tabname(`tabname') 
			
program drop mydisp
